package com.mxx.common.websecurity;



import com.mxx.tomcat.ResponseWriter;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author itzgyw
 * @Date 2020-04-11
 * @Time 16:42
 * @Description:
 */
public class AuthenticationResponseBodySuccessfulHandler implements AuthenticationSuccessHandler {
    private ResponseWriter responseWriter;
    private AuthenticationBodyHandler authenticationBodyHandler;

    public AuthenticationResponseBodySuccessfulHandler(ResponseWriter responseWriter, AuthenticationBodyHandler authenticationBodyHandler) {
        this.responseWriter = responseWriter;
        this.authenticationBodyHandler = authenticationBodyHandler;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        Object responseBody = authenticationBodyHandler.responseBodyOfSuccessful(httpServletRequest, authentication);
        responseWriter.writer(httpServletResponse, responseBody);
    }
}
